package com.soft.cake.controller;

import java.text.ParseException;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.soft.cake.pojo.User;
import com.soft.cake.service.IUserService;
import com.soft.cake.utils.DateUtil;
import com.soft.cake.utils.MD5EncoderUtil;

@Controller
public class UserController {

	@Autowired
	private IUserService userService;

	/**
	 * @description 根据主键查找用户
	 * @author Guo Xue
	 * @param //TODO please note like this
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.POST)
	public String selectUserByPrimaryKey(@PathVariable int id) {

		return "";
	}

	/**
	 * @description 根据主键修改用户
	 * @author Guo Xue
	 * @param //TODO please note like this
	 * @return
	 */
	@RequestMapping(value="update/{id}",method=RequestMethod.POST)	
	public String updateUserByPrimaryKey(@PathVariable int id,User user,@RequestParam("YYYY") String year,@RequestParam("MM") String month,@RequestParam("DD") String day) {	
		User user2 = userService.selectByPrimaryKey(id);
		user2.setUserUsername(user.getUserUsername());
		user2.setUserPassword(MD5EncoderUtil.getMD5Encoding(user.getUserPassword()));
		user2.setUserEmail(user.getUserEmail());
		user2.setUserPhone(user.getUserPhone());
		String birthdayString = year+"-"+month+"-"+day;
		Date userBirthday = DateUtil.strToDate(birthdayString);
		user2.setUserBirthday(userBirthday);
		//修改信息
		userService.updateByPrimaryKeySelective(user2);		
		return "redirect:/contact/"+String.valueOf(id);
	}

	/**
	 * @description 用户注册
	 * @author CanLig
	 * @param //TODO please note like this
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("/register")
	public String register(User user, HttpServletResponse response) {
		user.setUserBirthday(new Date());
		if (userService.register(user) == true)
			return "redirect:index";
		return "account";
	}
	
	/**
	 * @description 跳转到修改个人页面
	 * @author Guo Xue
	 * @param //TODO please note like this
	 * @return
	 * @throws ParseException
	 */
	 @RequestMapping(value="contact/{id}",method=RequestMethod.GET)
	 public String toModifyInfor(@PathVariable int id,Model model) throws ParseException{	
		 //根据id查找到该用户
		 User user = userService.selectByPrimaryKey(id);
		 model.addAttribute("user",user);
	     String birthday = DateUtil.date2String(user.getUserBirthday());
	     String year = birthday.substring(0, 4);
	     String month = birthday.substring(5, 7);
	     String day = birthday.substring(8, 10);		
		 model.addAttribute("birthday",year);
		 model.addAttribute("month",month);
		 model.addAttribute("day",day);		 
		 return "contact";
	 }
	 
}
